package org.dromara.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotUpkeepRecord;
import org.dromara.iot.domain.bo.IotUpkeepRecordBo;
import org.dromara.iot.domain.vo.IotUpkeepRecordVo;
import org.dromara.iot.mapper.IotUpkeepRecordMapper;
import org.dromara.iot.service.IIotUpkeepRecordService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 电梯保养记录Service业务层处理
 *
 * @author lsl
 * @date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class IotUpkeepRecordServiceImpl implements IIotUpkeepRecordService {

    private final IotUpkeepRecordMapper baseMapper;

    /**
     * 查询电梯保养记录
     */
    @Override
    public IotUpkeepRecordVo queryById(Long recordId) {
        return baseMapper.selectVoById(recordId);
    }

    /**
     * 查询电梯保养记录列表
     */
    @Override
    public TableDataInfo<IotUpkeepRecordVo> queryPageList(IotUpkeepRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IotUpkeepRecord> lqw = buildQueryWrapper(bo);
        Page<IotUpkeepRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电梯保养记录列表
     */
    @Override
    public List<IotUpkeepRecordVo> queryList(IotUpkeepRecordBo bo) {
        LambdaQueryWrapper<IotUpkeepRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotUpkeepRecord> buildQueryWrapper(IotUpkeepRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotUpkeepRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUpkeepId() != null, IotUpkeepRecord::getUpkeepId, bo.getUpkeepId());
        lqw.eq(bo.getReferUserId() != null, IotUpkeepRecord::getReferUserId, bo.getReferUserId());
        lqw.eq(bo.getAuditUserId() != null, IotUpkeepRecord::getAuditUserId, bo.getAuditUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditStatus()), IotUpkeepRecord::getAuditStatus, bo.getAuditStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getScoreMark()), IotUpkeepRecord::getScoreMark, bo.getScoreMark());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditIdea()), IotUpkeepRecord::getAuditIdea, bo.getAuditIdea());
        return lqw;
    }

    /**
     * 新增电梯保养记录
     */
    @Override
    public Boolean insertByBo(IotUpkeepRecordBo bo) {
        IotUpkeepRecord add = MapstructUtils.convert(bo, IotUpkeepRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRecordId(add.getRecordId());
        }
        return flag;
    }

    /**
     * 修改电梯保养记录
     */
    @Override
    public Boolean updateByBo(IotUpkeepRecordBo bo) {
        IotUpkeepRecord update = MapstructUtils.convert(bo, IotUpkeepRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotUpkeepRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电梯保养记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据保养ID查询保养记录
     *
     * @param upkeepId 保养ID
     * @return 保养记录
     */
    @Override
    public List<IotUpkeepRecordVo> queryListByUpkeepId(Long upkeepId) {
        LambdaQueryWrapper<IotUpkeepRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(IotUpkeepRecord::getUpkeepId, upkeepId);
        return baseMapper.selectVoList(wrapper);
    }

    /**
     * 查询当前审核人记录
     *
     * @param upkeepId
     * @param auditUserId
     * @return
     */
    @Override
    public IotUpkeepRecordVo selectAuditUserRecord(Long upkeepId, Long auditUserId) {
        LambdaQueryWrapper<IotUpkeepRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(IotUpkeepRecord::getUpkeepId, upkeepId);
        wrapper.eq(IotUpkeepRecord::getAuditUserId, auditUserId);
        return baseMapper.selectVoOne(wrapper);
    }
}
